<?php

namespace iBrand\Store\Backend\Repositories;

use iBrand\Store\Backend\Model\Goods;
use Prettus\Repository\Eloquent\BaseRepository;
use Prettus\Repository\Criteria\RequestCriteria;
use iBrand\Store\Backend\Model\Product;
use iBrand\Store\Backend\Model\Category;
use Maatwebsite\Excel\Facades\Excel;
use DB;

/**
 * Class ProductRepositoryEloquent
 *
 * @package namespace App\Repositories;
 */
class ProductRepository extends BaseRepository
{
    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return Product::class;
    }

    /**
     * Boot up the repository, pushing criteria
     */
    public function boot()
    {
        $this->pushCriteria(app(RequestCriteria::class));
    }

    /**
     * 购物车，根据商品ID，选择的商品规格json值获取货品数据
     *
     * @param $goods_id
     * @param $specJSON
     *
     * @return mixed
     */
    public function getProduct($goods_id, $specJSON)
    {
        $jsonData = json_decode($specJSON, true);
        if (!$jsonData) {
            return false;
        }
        //获取货品数据
        $product_info = $this->with('photo')->findWhere(['goods_id' => $goods_id, 'spec_array' => $specJSON, ['store_nums', '>', 0]])->first();
        //匹配到货品数据
        if (!$product_info) {
            return false;
        }

        return $product_info;
    }

    /**
     * 根据货品ID获取对应的商品
     *
     * @param $product_id
     *
     * @return mixed
     */
    public function getGoodsInfo($product_id)
    {
        $product = $this->findWhere(['id' => $product_id])->first();
        if ($product) {
            return $product;
        }

        return false;
    }

    /**
     * 根据货品SKU获取对应的商品
     *
     * @param $sku
     *
     * @return mixed
     */
    public function getGoodsInfoBySku($sku)
    {
        $product = $this->findWhere(['sku' => $sku])->first();
        if ($product) {
            return $product;
        }

        return false;
    }

    /**获取产品信息数据
     *
     * @param     $where
     * @param int $limit
     *
     * @return mixed
     */
    public function getProductPaginated($where, $limit = 50, $sku_price = '')
    {
        $date = $this->scopeQuery(function ($query) use ($where, $sku_price) {
            if (is_array($where) AND count($where) > 0) {
                foreach ($where as $key => $value) {
                    if (is_array($value)) {
                        list($operate, $va) = $value;
                        $query = $query->where($key, $operate, $va);
                    } else {
                        $query = $query->where($key, $value);
                    }
                }
            }

            if ($sku_price == 'sell_price' OR $sku_price == 'market_price') {
                $query = $query->where($sku_price, '>=', request('price_begin'));
                $query = $query->where($sku_price, '<=', request('price_end'));
            }

            return $query->orderBy('updated_at', 'desc');
        });

        if ($limit == 0) {
            return $date->all();
        } else {
            return $date->paginate($limit);
        }
    }

    /**
     * 获取第一个有库存的货品
     *
     * @param $goodsID
     *
     * @return mixed
     */
    public function getHaveStockProduct($goodsID)
    {
        $products = $this->with('photo')->findWhere(['goods_id' => $goodsID]);
        foreach ($products as $key => $val) {
            if ($val->store_nums > 0) {
                return $val;
            }
        }

        return false;
    }

    /**
     * 获取无库存的货品
     *
     * @param $specStr
     * @param $goodsID
     *
     * @return array
     */
    public function getNullStockProduct($specStr, $goodsID)
    {
        $products = $this->findWhere(['goods_id' => $goodsID]);
        $Stock = [
            'nullStock' => [],
            'haveStock' => [],
        ];
//        [0] => {"id":"1","type":"1","value":"WXS\/S","name":"尺码"}
//    [1] => {"id":"2","type":"1","value":"Ocean Blue","name":"颜色"}

        foreach ($products as $key => $val) {
            foreach ($specStr as $value) {
                if (strpos($val->spec_array, $value) >= 0) {

                    $spec = rtrim(str_replace($value, '', $val->spec_array), ']');
                    $spec = ltrim($spec, '[');

                    if (substr($spec, -1, 1) == ',') {
                        $spec = rtrim($spec, ',');
                    } elseif (substr($spec, 0, 1) == ',') {
                        $spec = ltrim($spec, ',');
                    }

                    if ($val->store_nums == 0) {
                        $Stock['nullStock'][] = $spec;
                    } else {
                        $Stock['haveStock'][] = $spec;
                    }
                }
            }
        }

        return $Stock;
    }

    /**
     * 根据SKU查询货品
     *
     * @param $sku
     *
     * @return mixed
     */
    public function getProductBySku($sku)
    {
        return $this->findWhere(['sku' => $sku])->first();
    }

    public function getExcelGoods($limit = 50)
    {
        $prefix = config('ibrand.app.database.prefix', 'ibrand_');

        $goodsTable = $prefix . 'base_goods';
        $productTable = $prefix . 'base_goods_product';
        $modelTable = $prefix . 'base_model';
        $products = Product::join($goodsTable, $goodsTable . '.id', '=', $productTable . '.goods_id')
            ->join($modelTable, $modelTable . '.id', '=', $goodsTable . '.model_id')
            ->select($goodsTable . '.id',
                $productTable . '.sku',
                $goodsTable . '.goods_no',
                $goodsTable . '.name',
                $goodsTable . '.market_price',
                $goodsTable . '.sell_price',
                $productTable . '.specID as spec1'
            );
        $view = !empty(request('view')) ? request('view') : 1;
        $products = $products->where($goodsTable . '.status', $view);

        if (is_array($ids = request('ids'))) {
            $products = $products->whereIn($goodsTable . '.id', $ids);
        }

        if (!empty(request('field')) && !empty(request('value'))) {
            if (request('field') == 'sku') {
                $products = $products->where($productTable . '.sku', 'like', '%' . request('value') . '%');
            } elseif (request('field') == 'category') {
                $goodsIds = $this->categoryGetGoodsIds(request('value'));
                $products = $products->whereIn($goodsTable . '.id', $goodsIds);
            } else {
                $products = $products->where($goodsTable . '.' . request('field'), 'like', '%' . request('value') . '%');
            }
        }
        if (!empty(request('store_begin'))) {
            $products = $products->where($goodsTable . '.store_nums', '>=', request('store_begin'));
        }
        if (!empty(request('store_end'))) {
            $products = $products->where($goodsTable . '.store_nums', '<=', request('store_end'));
        }
        if (!empty(request('price_begin'))) {
            if (request('price') == 'sku_sell_price' OR request('price') == 'sku_market_price') {
                $column = request('price') == 'sku_sell_price' ? 'sell_price' : 'market_price';
                $products = $products->where($productTable . '.' . $column, '>=', request('price_begin'));
            } else {
                $products = $products->where($goodsTable . '.' . request('price'), '>=', request('price_begin'));
            }
        }

        if (!empty(request('price_end'))) {
            if (request('price') == 'sku_sell_price' OR request('price') == 'sku_market_price') {
                $column = request('price') == 'sku_sell_price' ? 'sell_price' : 'market_price';
                $products = $products->where($productTable . '.' . $column, '<=', request('price_end'));
            } else {
                $products = $products->where($goodsTable . '.' . request('price'), '<=', request('price_end'));
            }
        }

        if (request('brand')) {
            $products = $products->where($goodsTable . '.brand_id', request('brand'));
        }

        if (request('year')) {
            $products = $products->where($goodsTable . '.year', request('year'));
        }

        if (request('season_id')) {
            $products = $products->where($goodsTable . '.season_id', request('season_id'));
        }

        if (!empty(request('goods_id_start'))) {
            $products = $products->where('goods_id', '>=', request('goods_id_start'));
        }

        if (!empty(request('goods_id_end'))) {
            $products = $products->where('goods_id', '<=', request('goods_id_end'));
        }


        $products = $products->orderBy($goodsTable . '.id')->paginate($limit);
        $lastPage = $products->lastPage();
        $products = $products->items();
        $specs = DB::table($prefix . 'base_specs_value')->join($prefix . 'base_spec', $prefix . 'base_spec.id', '=', $prefix . 'base_specs_value.spec_id')
            ->select($prefix . 'base_specs_value.id as id', $prefix . 'base_specs_value.spec_id as spec_id', $prefix . 'base_spec.name as spec', $prefix . 'base_specs_value.name as value')
            ->get();

        foreach ($products as $key => &$product) {
            $product = $product->toArray();

            $specIds = $product['spec1'];
            $specIds = json_decode($specIds, true);
            $products[$key]['spec1'] = '';
            $products[$key]['spec2'] = '';
            $products[$key]['spec3'] = '';
            if (is_array($specIds)) {
                foreach ($specIds as $id) {
                    $spec = $specs->where('id', $id)->first();
                    if ($spec) {
                        if ($spec->spec_id != 2) {
                            $products[$key]['spec1'] = $spec->value;
                        }
                        if ($spec->spec_id == 2) {
                            $products[$key]['spec2'] = $spec->value;
                        }

                        if ($spec->spec_id == 2) {
                            $alias = DB::table($prefix . 'base_goods_spec_relation')->where('spec_value_id', $spec->id)->where('goods_id', $product['id'])->first();
                            $products[$key]['spec3'] = isset($alias->alias) ? $alias->alias : '';
                        }
                    }
                }
            }
            $goods = Goods::find($product['id']);
            $cate = '';
            $categories = $goods->categories;
            foreach ($categories as $category) {
                $cate .= $category->name . ',';
            }
            $products[$key]['cate'] = $cate;
            $attr = '';

            /*$attributes = $goods->attr;
            if (count($attributes) > 0 AND !is_array($attributes)) {
                $attributes = $attributes->toArray();
            } elseif (!$attributes) {
                $attributes = [];
            }*/
            $attributes = [];


            foreach ($attributes as $key_ => $attribute) {
                $attr .= $attribute['name'] . ':' . $attribute['attribute_value'] . ',';
            }
            $products[$key]['attr'] = $attr;
        }

        return [
            'products' => $products,
            'lastPage' => $lastPage,
        ];
    }

    private function categoryGetGoodsIds($category)
    {
        $categoryIds = Category::where('name', 'like', '%' . $category . '%')->pluck('id')->toArray();
        $goods = Goods::with('categories')->get();

        return $goods->filter(function ($value, $key) use ($categoryIds) {
            foreach ($value->categories as $category) {
                if (in_array($category->id, $categoryIds)) {
                    return true;
                }
            }

            return false;
        })->pluck('id')->toArray();
    }

}
